package cz.acies.uniql.sql.element;

import org.dom4j.Element;
import org.dom4j.dom.DOMElement;


import cz.acies.uniql.dialect.Dialect;
import cz.acies.uniql.sql.UniQLExpr;
import cz.acies.utils.StringUtils;

/**
 * class UniQLValue je objekt nahrazuj�c� SQL v�razy typu 'NT00000001'.
 */
public class UniQLValue extends UniQLExpr
{
	public static final int TYPE_STRING = 0;
	public static final int TYPE_NUMBER = 1;
	public static final int TYPE_OTHER = 2;
	public static final int TYPE_TIMESTAMP = 3;

	/** The Constant TYPE_STRING. */	
	/** The Constant TYPE_NUMBER. */
	/** The Constant TYPE_OTHER. */	
	/** The Constant TYPE_TIMESTAMP. */
	
	private int type = -1;
	private String value;
	
	/**
	 * Konstruktor tridy UniQLValue.
	 */
	public UniQLValue() {
	}

	/**
	 * Konstruktor tridy UniQLValue.
	 * 
	 * @param dialect - dialect
	 */
	public UniQLValue(Dialect dialect) {
		super(dialect);
	}

	/**
	 * Konstruktor tridy UniQLValue.
	 * 
	 * @param value - value
	 */
	public UniQLValue(String value) {
		super();
		this.set(value);
	}

	/**
	 * Konstruktor tridy UniQLValue.
	 * 
	 * @param value - value
	 * @param type - type
	 */
	public UniQLValue(String value, int type) {
		super();
		this.setType(type);
		this.set(value);
	}
		
	// -----  -----------------------------------------------------------------
	
	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.element.UniQLExpr#translate()
	 */
	public String translate() throws Exception {
		return value;
	}
	
	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.element.UniQLExpr#parse(java.lang.String)
	 */
	public UniQLExpr parse(String sql) {
		return this;
	}

	// ----- -----------------------------------------------------------------

	/**
	 * Metoda odstran� v�echny doposud p�idan� elementy. Priv�tn� prom�bb� nastav� 
	 * na defauln� hodnoty.
	 *  
	 */
	public UniQLExpr clear() {
		value = null;
		return this;
	}

	
	/* (non-Javadoc)
	 * @see java.lang.Object#clone()
	 */
	@Override
	public Object clone() throws CloneNotSupportedException {
		Object clone = super.clone();
		assert clone instanceof UniQLValue;
		((UniQLValue) clone).set(value);
		return clone;
	}

	// -----  -----------------------------------------------------------------
	
	/**
	 * Navraci hodnotu promenne value.
	 * 
	 * @return vraci hodnotu promenne value.
	 */
	public String get() {
		return value;
	}

	/**
	 * Zadava hodnotu promenne value.
	 * 
	 * @param value - value
	 */
	public void set(String value) {
		switch (type) {
			case -1: {
				try {
					if (!StringUtils.isDigit(value.trim())) {
						if (value.trim().startsWith("'") && value.endsWith("")) {
						} else if (value.trim().equals("?")) {
						} else if (value.trim().startsWith("-") && value.endsWith("")) {
							if (!StringUtils.isNumeric(value.trim().substring(1))) {
								value = "'"+value+"'";
							} else {
								value = ""+value+"";
							}
						} else {
							if (!StringUtils.isNumeric(value.trim())) {
								value = "'"+value+"'";
							} else {
								value = ""+value+"";
							}
						}
					}					
				} catch (Exception exc) {
					value = ""+value+"";
				}
			}
			case UniQLValue.TYPE_STRING: {
				
				break;
			}
			case UniQLValue.TYPE_NUMBER: {
				
				break;
			}
			case UniQLValue.TYPE_TIMESTAMP: {
				try {
					value = getDialect().getValueTimestamp(value);	
				} catch (Exception exc) {
					exc.printStackTrace();
				}
				break;
			}
			case UniQLValue.TYPE_OTHER: {
				
				break;
			}

		}
		this.value = value;
	}

	// -----  -----------------------------------------------------------------
	
	/**
	 * Navraci hodnotu promenne type.
	 * 
	 * @return - vraci hodnotu type
	 */
	public int getType() {
		return type;
	}

	/**
	 * Zada hodnotu promenne type.
	 * 
	 * @param type nova hodnota type
	 */
	public void setType(int type) {
		this.type = type;
	}
	
	// -----  -----------------------------------------------------------------
	
	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#transToXML()
	 */
	public String transToXML() throws Exception {
		DOMElement node = new DOMElement("UniQLValue");
		node.addAttribute("value", value);
		return node.asXML();
	}
	
	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#transToXML(org.dom4j.dom.DOMElement)
	 */
	public DOMElement transToXML(DOMElement node) throws Exception {
		node.setName("UniQLValue");
		if (value != null) {
			node.setAttribute("value", value);
		}
		return node;
	}
	
	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#xmlToExpr(java.lang.String)
	 */
	public UniQLExpr xmlToExpr(String xml) throws Exception {
		return this;
	}

	/* (non-Javadoc)
	 * @see cz.acies.uniql.sql.UniQLExpr#xmlToExpr(org.dom4j.Element)
	 */
	public UniQLExpr xmlToExpr(Element node) throws Exception {
		String s1 = node.attributeValue("value");
		if (s1 != null && s1.length() > 0) this.set(s1);
		return this;
	}
	
	// -----  -----------------------------------------------------------------
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return "UniQLValue( "+this.value+" )";
	}
}
